package com.peng;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class controller {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/userList")
    public List<Map<String,Object>> userList(){
        String sql="select * from mybatis.user";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }

    @RequestMapping("/addUser")
    public String adduser(){
        String sql = "insert into mybatis.user values(5,'狂5','123456')";
        jdbcTemplate.update(sql);
        return "insert";
    }

    @RequestMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id") int id){
        String sql = "update mybatis.user set name=?, pwd=? where id="+id;
        Object[] objects=new Object[2];
        objects[0]="狂555";
        objects[1]="111111";
        jdbcTemplate.update(sql ,objects);
        return "update";
    }

    @RequestMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") int id){
        String sql = "delete from mybatis.user where id="+id;
        jdbcTemplate.update(sql);
        return "delete";
    }
}
